package com.trade.common.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: WebUitl
 * @Description: Web处理常用方法
 * @author 曹文波
 * @date 2014年2月24日 上午17:00:06
 * 
 */

public class WebUitl {
	
	
	private static String SORT_ASC="ASC";
	@SuppressWarnings("unused")
	private static String SORT_DESC="DESC";

	/**
	 * 构造分页参数
	 * 
	 * @param pageNumber
	 * 			要查询的页码(默认为第1页)
	 * @param pageSize
	 *            页面大小(默认为10条)
	 * @param sortStr
	 *            排序字段数组字符串,默认值为 [{"sortCol":"createTime"},{"sortby":"DESC"}]
	 * @return
	 */
	public static PageRequest buildPageRequest(Integer pageNumber, Integer pageSize,
			String sortStr) {
		List<Order> orders = new ArrayList<Order>();
		//设置默认的pageSize
		if (XaUtil.isEmpty(pageSize)) {
			pageSize = 10;
		}
		//设置默认的pageNumber
		if (XaUtil.isEmpty(pageNumber)) {
			pageNumber = 1;
		}

		//设置排序条件，默认使用更新时间倒序排列
		if (XaUtil.isNotEmpty(sortStr)) {
			JSONArray sortJsonArry = JSONArray.fromObject(sortStr);
			for (int i = 0; i < sortJsonArry.size(); i++) {
				JSONObject pageSortJson = JSONObject.fromObject(sortJsonArry.get(i));
				PageSort pageSort = (PageSort) JSONObject.toBean(pageSortJson, PageSort.class);
				if(SORT_ASC.equalsIgnoreCase(pageSort.getDirection())){
					orders.add(new Order(Sort.Direction.ASC, pageSort.getProperty()));
				}else{
					orders.add(new Order(Sort.Direction.DESC, pageSort.getProperty()));
				}
			}
		}
		if (XaUtil.isEmpty(orders)){
			orders.add(new Order(Sort.Direction.DESC, "createTime"));
		}
		Sort sorts = new Sort(orders);
		PageRequest pageRequest = new PageRequest(pageNumber , pageSize,sorts);
		return pageRequest;
	}

	/**
	 * 构造分页参数
	 * @param sortStr
	 * @return
	 */
	public static Sort buildSort(String sortStr) {
		List<Order> orders = new ArrayList<Order>();
		//设置排序条件，默认使用更新时间倒序排列
		if (XaUtil.isNotEmpty(sortStr)) {
			JSONArray sortJsonArry = JSONArray.fromObject(sortStr);
			for (int i = 0; i < sortJsonArry.size(); i++) {
				JSONObject pageSortJson = JSONObject.fromObject(sortJsonArry.get(i));
				PageSort pageSort = (PageSort) JSONObject.toBean(pageSortJson, PageSort.class);
				if(SORT_ASC.equalsIgnoreCase(pageSort.getDirection())){
					orders.add(new Order(Sort.Direction.ASC, pageSort.getProperty()));
				}else{
					orders.add(new Order(Sort.Direction.DESC, pageSort.getProperty()));
				}
			}
		}
		if (orders.size() > 0){
			return new Sort(orders);
		}else{
			orders.add(new Order(Sort.Direction.DESC, "createTime"));
			return new Sort(orders);
		}
	}


	/**
	 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
	 * 
	 * 返回的结果的Parameter名已去除前缀.
	 */
/*	@SuppressWarnings("unchecked")
	public static Map<String, Object> getParametersStartingWith(
			String jsonFilter, String prefix) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		if (XaUtil.isNotEmpty(jsonFilter) && XaUtil.isNotEmpty(prefix)) {
			Map<String, String> map  = JSONObject.fromObject(jsonFilter);
			//Map<String, String> map = JSON.parseObject(jsonFilter,HashMap.class);
			for (String key : map.keySet()) {
				if (key.startsWith(prefix)) {
					String unprefixed = key.substring(prefix.length());
					String value = map.get(key);
					searchParams.put(unprefixed, value);
				}
			}
		}
		return searchParams;
	}*/
}
